const {SportAdvice} = require('@/models/sport');
const { clearObject } = require('@/utils');

const getSportAdvices = async (req, res) => {
  try {
    const { user_id, plan_id, is_read, user_name, pageNum, pageSize } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const sportAdvices = await SportAdvice.getSportAdvices(user_id, plan_id, is_read, user_name, offset, limit);
    res.status(200).send(sportAdvices);
  } catch (error) {
    res.status(500).json({ message: '获取运动建议失败', error });
  }
}
const createSportAdvices = async (req, res) => {
  try {
    const body = req.body;
    const params = clearObject(body);
    const sportAdvices = await SportAdvice.createSportAdvices({...params, generate_time: new Date()});
    res.status(200).send(sportAdvices);
  } catch (error) {
    res.status(500).json({ message: '创建运动建议失败', error });
  }
}
const updateSportAdvices = async (req, res) => {
  try {
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const sportAdvices = await SportAdvice.updateSportAdvices(id, params);
    res.status(200).send(sportAdvices);
  } catch (error) {
    res.status(500).json({ message: '更新运动建议失败', error });
  }
}

module.exports = {
  getSportAdvices,
  createSportAdvices,
  updateSportAdvices
}
